var app = getApp();
Page({
    data:{
      orderId:0,
      goodsList:[],
      yunPrice:"0.00",
      statusType: ["", "待付款", "待发货", "待收货", "待评价", "", "", "", "", "", "","待发货","等待开团成功后支付","开团成功待支付"],
      appid: app.globalData.appid,
      remark:"",
    },
    onLoad:function(e){
      var orderId = e.id;
      this.data.orderId = orderId;
      this.setData({
        orderId: orderId
      });
    },
    onShow : function () {
      var that = this;
      wx.request({
        // url: 'https://api.it120.cc/' + app.globalData.subDomain + '/order/detail',
        // data: {
        //   token: wx.getStorageSync('token'),
        //   id: that.data.orderId
        // },
        url: app.globalData.domain + '/wxapp/order/' + that.data.orderId + '/',
        header: {
          'Authorization': 'JWT ' + wx.getStorageSync('token')
        },
        success: (res) => {
          wx.hideLoading();
          if (res.statusCode != 200) {
            wx.showModal({
              title: '错误',
              content: res.data.msg,
              showCancel: false
            })
            return;
          }
          that.setData({
            orderDetail: res.data
          });
        }
      })
      var yunPrice = parseFloat(this.data.yunPrice);
      var allprice = 0;
      var goodsList = this.data.goodsList;
      for (var i = 0; i < goodsList.length; i++) {
        allprice += parseFloat(goodsList[0].price) * goodsList[0].number;
      }
      this.setData({
        allGoodsPrice: allprice,
        yunPrice: yunPrice
      });
    },
    wuliuDetailsTap:function(e){
      var orderId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/wuliu/index?id=" + orderId
      })
    },
    confirmBtnTap:function(e){
      let that = this;
      let orderId = this.data.orderId;
      let formId = e.detail.formId;
      wx.showModal({
          title: '确认您已收到商品？',
          content: '',
          success: function(res) {
            if (res.confirm) {
              wx.showLoading();
              wx.request({
                // url: 'https://api.it120.cc/' + app.globalData.subDomain + '/order/delivery',
                // data: {
                //   token: wx.getStorageSync('token'),
                //   orderId: orderId
                // },
                url: app.globalData.domain + '/wxapp/order/' + orderId + '/',
                header: {
                  'Authorization': 'JWT ' + wx.getStorageSync('token')
                },
                method: 'PUT',
                data:{
                  order_status: 4,
                  wxapp_suborder_set: that.data.orderDetail.wxapp_suborder_set
                },
                success: (res) => {
                  if (res.statusCode == 200) {
                    that.onShow();
                    // 模板消息，提醒用户进行评价
                    let postJsonString = {};
                    // postJsonString.keyword1 = { value: that.data.orderDetail.orderInfo.orderNumber, color: '#173177' }
                    postJsonString.keyword1 = { value: that.data.orderDetail.order_no, color: '#173177' }
                    let keywords2 = '您已确认收货，期待您的再次光临！';
                    if (app.globalData.order_reputation_score) {
                      keywords2 += '立即好评，系统赠送您' + app.globalData.order_reputation_score +'积分奖励。';
                    }
                    postJsonString.keyword2 = { value: keywords2, color: '#173177' }
                    // app.sendTempleMsgImmediately('uJL7D8ZWZfO29Blfq34YbuKitusY6QXxJHMuhQm_lco', formId,
                    //   '/pages/order-details/index?id=' + orderId, JSON.stringify(postJsonString));
                  }
                }
              })
            }
          }
      })
    },
    submitReputation: function (e) {
      let that = this;
      // let formId = e.detail.formId;
      // let postJsonString = {};
      // postJsonString.token = wx.getStorageSync('token');
      // postJsonString.orderId = this.data.orderId;
      // let reputations = [];
      if(e.detail.value["orderGoodsId"]) {
        var orderGoodsId = e.detail.value["orderGoodsId"];
        var goodReputation = e.detail.value["goodReputation"];
        var goodReputationRemark = e.detail.value["goodReputationRemark"];
        //console.log(goodReputationRemark);
        // var reputations_json = {};
        // reputations_json.sub_order = orderGoodsId;
        // reputations_json.item_judge = goodReputation;
        // reputations_json.comment = goodReputationRemark;
        // reputations.push(reputations_json);
      }
      
      // console.log(reputations_json.sub_order, reputations_json.item_judge, reputations_json.comment);
      //postJsonString.reputations = reputations;
      wx.showLoading();
      wx.request({
        //url: 'https://api.it120.cc/' + app.globalData.subDomain + '/order/reputation',
        url: app.globalData.domain + '/wxapp/judge/',
        method: 'POST',
        header: {
          //'content-type': 'application/x-www-form-urlencoded', 
          'Authorization': 'JWT ' + wx.getStorageSync('token')
        },
        data: {
          //postJsonString: postJsonString
          sub_order: orderGoodsId,
          item_judge: goodReputation,
          comment: goodReputationRemark,
        },
        success: (res) => {
          console.log(res.data);
          that.setData({
            remark:res.data.comment,
          });
          wx.hideLoading();
          if (res.statusCode == 200 || res.statusCode == 201) {
            that.onShow();
            // 模板消息，通知用户已评价
            // let postJsonString = {};
            // postJsonString.keyword1 = { value: that.data.orderDetail.orderInfo.orderNumber, color: '#173177' }
            // let keywords2 = '感谢您的评价，期待您的再次光临！';
            // if (app.globalData.order_reputation_score) {
            //   keywords2 += app.globalData.order_reputation_score + '积分奖励已发放至您的账户。';
            // }
            // postJsonString.keyword2 = { value: keywords2, color: '#173177' }
           // app.sendTempleMsgImmediately('uJL7D8ZWZfO29Blfq34YbuKitusY6QXxJHMuhQm_lco', formId,
           //  '/pages/order-details/index?id=' + that.data.orderId, JSON.stringify(postJsonString));
          }
        }
      })
    }
})